<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.version"
          placeholder="请输入版本号"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.reshFlag"
          placeholder="请选择更新类型"
        >
          <el-option label="强制更新" value="1"></el-option>
          <el-option label="非强制更新" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.appType"
          placeholder="请选择运行系统"
        >
          <el-option label="Android" value="1"></el-option>
          <el-option label="IOS" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加版本</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="version" label="版本号"> </el-table-column>
        <el-table-column prop="reshFlag" label="更新类型">
          <template slot-scope="scope">
            <span v-if="scope.row.reshFlag == 1">强制更新</span>
            <span v-if="scope.row.reshFlag == 2">非强制更新</span>
          </template>
        </el-table-column>
        <el-table-column prop="newVersion" label="最新版本">
          <template slot-scope="scope">
            <span v-if="scope.row.newVersion == 1">是</span>
            <span v-if="scope.row.newVersion == 2">否</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="更新内容">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.remark"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.remark }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="appType" label="运行系统">
          <template slot-scope="scope">
            <span v-if="scope.row.appType == 1">Android</span>
            <span v-if="scope.row.appType == 2">IOS</span>
          </template>
        </el-table-column>
        <el-table-column label="APK包">
          <template slot-scope="scope">
            <span v-if="scope.row.appType == 1"> </span>
            <el-link
              v-if="scope.row.appType == 1"
              type="primary"
              @click="downloadFile(scope.row.appFileVersion.url)"
              >{{ appFileVersion.name }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="addName" label="发布人"></el-table-column>
        <el-table-column
          prop="addTime"
          label="更新时间"
          width="160px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="版本号" required>
          <el-input
            v-model.trim="form.version"
            clearable
            maxlength="20"
            placeholder="请输入版本号"
          ></el-input>
        </el-form-item>
        <el-form-item label="更新类型" required>
          <el-select
            clearable
            v-model.trim="form.reshFlag"
            placeholder="请选择更新类型"
            style="width: 100%"
          >
            <el-option label="强制更新" value="1"></el-option>
            <el-option label="非强制更新" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最新版本" required>
          <el-select
            clearable
            v-model.trim="form.newVersion"
            placeholder="请选择是否最新版本"
            style="width: 100%"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运行系统" required>
          <el-select
            clearable
            v-model.trim="form.appType"
            placeholder="请选择运行系统"
            style="width: 100%"
          >
            <el-option label="Android" value="1"></el-option>
            <el-option label="IOS" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="APK包" required v-if="form.appType == 1">
          <web-upload
            @upload="upload"
            type="file"
            maxSize="100"
            accept="application/vnd.android.package-archive"
          />
          <el-link
            v-if="appFileVersion.url"
            type="primary"
            @click="downloadFile(appFileVersion.url)"
            >{{ appFileVersion.name }}</el-link
          >
        </el-form-item>
        <el-form-item label="更新内容" required>
          <el-input
            type="textarea"
            v-model.trim="form.remark"
            placeholder="请输入更新内容"
            maxlength="200"
            :rows="7"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="版本号">
          {{ lockData.version }}
        </el-descriptions-item>
        <el-descriptions-item label="更新类型">
          {{ lockData.reshFlag == 1 ? '强制更新' : '非强制更新' }}
        </el-descriptions-item>
        <el-descriptions-item label="最新版本">
          {{ lockData.newVersion == 1 ? '是' : '否' }}
        </el-descriptions-item>
        <el-descriptions-item label="运行系统">
          {{ lockData.appType == 1 ? 'Android' : 'IOS' }}
        </el-descriptions-item>
        <el-descriptions-item label="发布人">
          {{ lockData.addName }}
        </el-descriptions-item>
        <el-descriptions-item label="更新时间">
          {{ lockData.addTime }}
        </el-descriptions-item>
        <el-descriptions-item label="更新内容" :span="2">
          {{ lockData.remark }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加版本',
      form: {},
      appFileVersion: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/version/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.title = '添加版本'
      this.form = {}
      this.appFileVersion = {}
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios.get('/admin/version/find?id=' + row.id).then((data) => {
            this.lockData = {}
            if (data.code == 200) {
              this.lockData = data.data
              this.isLook = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'edit':
          this.title = '编辑版本'
          this.$axios.get('/admin/version/find?id=' + row.id).then((data) => {
            this.form = {}
            this.appFileVersion = {}
            if (data.code == 200) {
              this.form = data.data
              this.appFileVersion = data.data.appFileVersion
              this.visible = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/admin/version/del?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.version) {
        this.$message.error('请输入版本号')
        return
      }
      if (!this.form.reshFlag) {
        this.$message.error('请选择更新类型')
        return
      }
      if (!this.form.newVersion) {
        this.$message.error('请选择是否最新版本')
        return
      }
      if (!this.form.appType) {
        this.$message.error('请选择运行系统')
        return
      }
      if (!this.form.appFileVersion && this.form.appType == 1) {
        this.$message.error('请上传APK包')
        return
      }
      if (this.form.appType == 2) {
        this.form.appFileVersion = {}
      }
      if (!this.form.remark) {
        this.$message.error('请输入更新内容')
        return
      }
      this.$axios.post('/admin/version/add', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    upload(data, md5) {
      //上传
      this.form.appFileVersion = {
        md5: md5,
        name: data.name,
        size: data.size,
        url: data.fileDomain + data.url,
      }
      this.appFileVersion = {
        md5: md5,
        name: data.name,
        size: data.size,
        url: data.fileDomain + data.url,
      }
    },
    downloadFile(url) {
      //下载附件
      const a = document.createElement('a')
      a.href = url
      a.click()
    },
  },
}
</script>
